package com.ruoyi.web.controller.project;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.*;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ProjectManagement;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 项目管理Controller
 *
 * @author zhaonz
 * @date 2023-06-02
 */
@Controller
@RequestMapping("/project/management")
public class ProjectManagementController extends BaseController {
    private String prefix = "project/management";

    @Autowired
    private IProjectManagementService projectManagementService;

    @Autowired
    private IProjectScheduleService projectScheduleService;

    @Resource
    private RuntimeService runtimeService;

    @Resource
    private TaskService taskService;

    @RequiresPermissions("system:management:view")
    @GetMapping()
    public String Management() {
        return prefix + "/management";
    }

    /**
     * 查询项目管理列表
     */
    @RequiresPermissions("system:management:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProjectManagement projectManagement) {
        startPage();
        List<ProjectManagement> list = projectManagementService.list(projectManagement);
        return getDataTable(list);
    }

    /**
     * 导出项目管理列表
     */
    @RequiresPermissions("system:management:export")
    @Log(title = "项目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProjectManagement projectManagement) {
        List<ProjectManagement> list = projectManagementService.list(projectManagement);
        ExcelUtil<ProjectManagement> util = new ExcelUtil<ProjectManagement>(ProjectManagement.class);
        return util.exportExcel(list, "项目管理数据");
    }

    /**
     * 新增项目管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        SysUser user = super.getSysUser();
        mmap.put("user", user);
        return prefix + "/add";
    }

    /**
     * 新增保存项目管理
     */
    @RequiresPermissions("system:management:add")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProjectManagement projectManagement) {
        return toAjax(projectManagementService.save(projectManagement));
    }

    /**
     * 详细
     *
     * @param projectId
     * @param mmap
     * @return
     */
    @RequiresPermissions("system:management:detail")
    @GetMapping("/detail/{projectId}")
    public String detail(@PathVariable("projectId") Long projectId, ModelMap mmap)
    {
        mmap.put("detectionId", Long.toString(projectId));
        ProjectManagement detail = projectManagementService.detail(projectId);
        mmap.put("management", detail);
        return prefix + "/detail";
    }

    /**
     * 修改项目管理
     */
    @RequiresPermissions("system:management:edit")
    @GetMapping("/edit/{projectId}")
    public String edit(@PathVariable("projectId") Long projectId, ModelMap mmap) {
        ProjectManagement projectManagement = projectManagementService.getById(projectId);
        mmap.put("projectManagement", projectManagement);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目管理
     */
    @RequiresPermissions("system:management:edit")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProjectManagement projectManagement) {
        return toAjax(projectManagementService.updateById(projectManagement));
    }

    /**
     * 删除项目管理
     */
    @RequiresPermissions("system:management:remove")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(projectManagementService.removeByIds(ids));
    }

    /**
     * 需求调研
     */
    @GetMapping("/research")
    public String research(String taskid, ModelMap mmap) {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        Map<String, Object> variablesv = runtimeService.getVariables(t.getExecutionId());
        mmap.put("taskid", taskid);
        mmap.put("projectId", variablesv.get("projectId").toString());
        mmap.put("createBy", variablesv.get("createBy").toString());
        mmap.put("scheduleId", variablesv.get("scheduleId").toString());

        return prefix + "/research";
    }

    /**
     * 产品委审核
     */
    @GetMapping("/productaudit")
    public String productaudit(String taskid, ModelMap mmap) {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        Map<String, Object> variablesv = runtimeService.getVariables(t.getExecutionId());
        mmap.put("taskid", taskid);
        mmap.put("projectId", variablesv.get("projectId").toString());
        mmap.put("createBy", variablesv.get("createBy").toString());
        mmap.put("scheduleId", variablesv.get("scheduleId").toString());

        return prefix + "/productaudit";
    }

    /**
     * 信科委审核
     */
    @GetMapping("/Informationaudit")
    public String informationaudit(String taskid, ModelMap mmap) {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        Map<String, Object> variablesv = runtimeService.getVariables(t.getExecutionId());
        mmap.put("taskid", taskid);
        mmap.put("projectId", variablesv.get("projectId").toString());
        mmap.put("createBy", variablesv.get("createBy").toString());
        mmap.put("scheduleId", variablesv.get("scheduleId").toString());

        return prefix + "/Informationaudit";
    }

    /**
     * POC测试
     */
    @GetMapping("/poctesting")
    public String poctesting(String taskid, ModelMap mmap) {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        Map<String, Object> variablesv = runtimeService.getVariables(t.getExecutionId());
        mmap.put("taskid", taskid);
        mmap.put("projectId", variablesv.get("projectId").toString());
        mmap.put("createBy", variablesv.get("createBy").toString());
        mmap.put("scheduleId", variablesv.get("scheduleId").toString());

        return prefix + "/poctesting";
    }

    /**
     * 立项审核
     */
    @GetMapping("/approvalaudit")
    public String approvalaudit(String taskid, ModelMap mmap) {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        Map<String, Object> variablesv = runtimeService.getVariables(t.getExecutionId());
        mmap.put("taskid", taskid);
        mmap.put("projectId", variablesv.get("projectId").toString());
        mmap.put("createBy", variablesv.get("createBy").toString());
        mmap.put("scheduleId", variablesv.get("scheduleId").toString());

        return prefix + "/approvalaudit";
    }

    /**
     * 行党委审核
     */
    @GetMapping("/executivepartyaudit")
    public String executivepartyaudit(String taskid, ModelMap mmap) {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        Map<String, Object> variablesv = runtimeService.getVariables(t.getExecutionId());
        mmap.put("taskid", taskid);
        mmap.put("projectId", variablesv.get("projectId").toString());
        mmap.put("createBy", variablesv.get("createBy").toString());
        mmap.put("scheduleId", variablesv.get("scheduleId").toString());

        return prefix + "/executivepartyaudit";
    }

    /**
     * 财审会审核
     */
    @GetMapping("/financialreviewaudit")
    public String financialreviewaudit(String taskid, ModelMap mmap) {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        Map<String, Object> variablesv = runtimeService.getVariables(t.getExecutionId());
        mmap.put("taskid", taskid);
        mmap.put("projectId", variablesv.get("projectId").toString());
        mmap.put("createBy", variablesv.get("createBy").toString());
        mmap.put("scheduleId", variablesv.get("scheduleId").toString());

        return prefix + "/financialreviewaudit";
    }

    /**
     * 招标
     */
    @GetMapping("/invitebids")
    public String invitebids(String taskid, ModelMap mmap) {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        Map<String, Object> variablesv = runtimeService.getVariables(t.getExecutionId());
        mmap.put("taskid", taskid);
        mmap.put("projectId", variablesv.get("projectId").toString());
        mmap.put("createBy", variablesv.get("createBy").toString());
        mmap.put("scheduleId", variablesv.get("scheduleId").toString());

        return prefix + "/invitebids";
    }

    /**
     * 签订合同
     */
    @GetMapping("/concludeacontract")
    public String concludeacontract(String taskid, ModelMap mmap) {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        Map<String, Object> variablesv = runtimeService.getVariables(t.getExecutionId());
        mmap.put("taskid", taskid);
        mmap.put("projectId", variablesv.get("projectId").toString());
        mmap.put("createBy", variablesv.get("createBy").toString());
        mmap.put("scheduleId", variablesv.get("scheduleId").toString());

        ProjectManagement management = projectManagementService.getById((Long) variablesv.get("projectId"));
        mmap.put("projectName", management.getProjectName());
        return prefix + "/concludeacontract";
    }

    /**
     * 付款
     */
    @GetMapping("/payment")
    public String payment(String taskid, ModelMap mmap) {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        Map<String, Object> variablesv = runtimeService.getVariables(t.getExecutionId());
        mmap.put("taskid", taskid);
        mmap.put("projectId", variablesv.get("projectId").toString());
        mmap.put("createBy", variablesv.get("createBy").toString());
        mmap.put("scheduleId", variablesv.get("scheduleId").toString());

        return prefix + "/payment";
    }

    /**
     * 验收
     */
    @GetMapping("/checkandaccept")
    public String checkandaccept(String taskid, ModelMap mmap) {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        Map<String, Object> variablesv = runtimeService.getVariables(t.getExecutionId());
        mmap.put("taskid", taskid);
        mmap.put("projectId", variablesv.get("projectId").toString());
        mmap.put("createBy", variablesv.get("createBy").toString());
        mmap.put("scheduleId", variablesv.get("scheduleId").toString());

        return prefix + "/checkandaccept";
    }
}
